package com.rbac.controller;

import java.util.List;

import javax.sql.DataSource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rbac.bean.Msg;
import com.rbac.bean.Permission;
import com.rbac.bean.User;
import com.rbac.dao.UserDao;
import com.rbac.util.WebUtil;

@Controller
public class LoginController {
	
	@RequestMapping("login")
	@ResponseBody
	public Msg login(User user) {
		Msg msg=Msg.success;
		Subject subject = SecurityUtils.getSubject();
		if(!subject.isAuthenticated()) {
			UsernamePasswordToken token=new UsernamePasswordToken(user.getUsername(), user.getPassword());
			if(user.isRememberMe()) {
				token.setRememberMe(true);
			}
			try {
				subject.login(token);
				System.out.println("登录成功");
				
			} catch (Exception e) {
				System.out.println("登录失败,原因如下：");
				e.printStackTrace();
				msg=Msg.fail;
			}
		}
		return msg;
	}

}
